package com.wontube.smr.dao.bo;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.extension.utils.DateUtils;
import com.wontube.client.extension.utils.StringUtils;
import com.wontube.core.dao.mybatis.Criteria;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.dao.mapper.web.ViewHistoryMapper;
import com.wontube.smr.pub.DefaultDBHelper;
import com.wontube.smr.web.client.model.po.ViewHistory;

/**
 * @author tanggf(2012-5-30)
 *
 */
public class ViewHistoryDao {
	public static List<ViewHistory> listLatestViewHistories(String session, int limit){
		SqlSession sqlSession = DefaultDBHelper.getSqlSession(true);
		try {
			ViewHistoryMapper mapper = sqlSession.getMapper(ViewHistoryMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("session=", session);
			ex.setOrderByClause("view_time desc");
			return mapper.selectByExample(ex, new RowBounds(0, limit));
		} finally {
			sqlSession.close();
		}
	}	
	
	public static int addOrUpdateViewHistory(ViewHistory history){
		SqlSession sqlSession = DefaultDBHelper.getSqlSession(true);
		try {
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("session=", history.getSession())
					.addCriterion("title=", history.getTitle())
					.addCriterion("type=", history.getType());
			ViewHistoryMapper mapper = sqlSession.getMapper(ViewHistoryMapper.class);			
			List<ViewHistory> hss = mapper.selectByExample(ex);
			if(hss.size() > 0){
				//update
				ViewHistory temp = new ViewHistory();
				temp.setId(hss.get(0).getId());
				temp.setViewTime(DateUtils.getNow());
				return mapper.updateByPrimaryKeySelective(temp);
			}else{
				//add
				history.setId(UUID.randomUUID().toString());
				history.setViewTime(DateUtils.getNow());
				return mapper.insert(history);
			}
		} finally {
			sqlSession.close();
		}
	}
	
	public static int deleteAllViewHistory(String session){
		return deleteViewHistory(session, null);
	}
	
	public static int deleteViewHistory(String session, String id){
		SqlSession sqlSession = DefaultDBHelper.getSqlSession(true);
		try {
			ViewHistoryMapper mapper = sqlSession.getMapper(ViewHistoryMapper.class);
			QueryExample ex = new QueryExample();
			Criteria cri = ex.or().addCriterion("session=", session);
			if(id != null){
				 cri.addCriterion("id=", id);
			}
					
			return mapper.deleteByExample(ex);
		} finally {
			sqlSession.close();
		}
	}		
	
	/**
	 * 定时清除过期记录, 由定时器调用.
	 * @param day 过期天数
	 */
	public static int cleanupViewHistories(int day){		
		if(day <= 0){
			return 0;
		}
		int totalAff = 0;	
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_YEAR, - day);
		String condition = StringUtils.format("view_time < '{}'", 
				new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
		QueryExample ex = new QueryExample();
		ex.or().addCriterion(condition);
		
		SqlSession session = DefaultDBHelper.getSqlSession(false);
		try {
			ViewHistoryMapper mapper = session.getMapper(ViewHistoryMapper.class);
			totalAff = mapper.deleteByExample(ex);		
			session.commit();			
		} finally {
			session.close();
		}			
		return totalAff;
	}
}
